import wx
from wx import aui
import  wx.lib.rcsizer  as rcs


class notepad(wx.Frame):
    
    def __init__(self):
        wx.Frame.__init__(self, None, -1, "Notepad", size=(800,600))
        self.panel = wx.Panel(self)

        self.nb = aui.AuiNotebook(self.panel,size=(800,600))
        self.flagAddTab = False
        page = wx.TextCtrl(self.nb, -1, "sdfsd", style=wx.TE_MULTILINE)
        self.nb.AddPage(page, "Welcome")
        self.bm_end = wx.Bitmap("image/plus1.png", wx.BITMAP_TYPE_ANY) 
#        id = wx.Image("image/connect.png", wx.BITMAP_TYPE_ANY)
#        il = wx.ImageList(16, 16)
#        idx1 = il.Add(self.bm_end)
#        self.nb.AssignImageList(il)
#        active_tabctrl = self.nb.GetActiveTabCtrl()
#        active_tabctrl.AddButton(-1, wx.RIGHT, wx.Bitmap("image/plus2.png"))
        auiManager = self.nb.GetAuiManager()#auiManager.GetAllPanes()[0]
        
        self.nb.AddPage(wx.StaticText(self,-1,""), "",bitmap = wx.Bitmap("image/plus2.png", wx.BITMAP_TYPE_ANY))
        self.nb.SetTabCtrlHeight(35)
        self.nb.SetWindowStyle(aui.AUI_NB_CLOSE_ON_ACTIVE_TAB);
        self.nb.Bind(aui.EVT_AUINOTEBOOK_PAGE_CHANGING, self.chagedTab)
        self.nb.Bind(aui.EVT_AUINOTEBOOK_PAGE_CHANGED, self.chageTab)
        self.nb.Bind(aui.EVT_AUINOTEBOOK_PAGE_CLOSED, self.tabClose)
        
        sizer = wx.BoxSizer()
        sizer.Add(self.nb, 1, wx.EXPAND)
        self.panel.SetSizer(sizer)
    
    def openTab(self,event):
        panel = wx.Panel(self.nb,-1)
        grid1 =  rcs.RowColSizer()
        txt = wx.TextCtrl(panel, -1, "This is page " ,
                               style=wx.TE_MULTILINE)
        saveBnt = wx.Button(panel, label = "Save")
        
        grid1.Add(txt,row=1, col=1,flag=wx.EXPAND)
        grid1.Add(saveBnt,row=2, col=1)        
        panel.SetSizer(grid1) 
        self.nb.InsertPage(self.nb.GetPageCount()-1, panel, "new")
        #self.nb.SetWindowStyle(aui.AUI_NB_CLOSE_ON_ACTIVE_TAB);
#        self.nb.AddPage(wx.StaticText(self,-1,""), "",bitmap = self.bm_end)
    
    def chageTab(self,event):
        print "chageTab", self.nb.GetPageCount(),event.GetSelection(), self.nb.GetSelection()
        if(event.GetSelection()==0):
            self.nb.SetWindowStyle(aui.AUI_NB_TOP | aui.AUI_NB_TAB_SPLIT | aui.AUI_NB_TAB_MOVE | aui.AUI_NB_SCROLL_BUTTONS);
            event.Skip()
        else:
            if self.nb.GetPageCount() == event.GetSelection()+2:
                print 'aaa'
                self.nb.SetToolTip(wx.ToolTip("adfad"))
#                self.nb.SetWindowStyle(aui.AUI_NB_RIGHT | aui.AUI_NB_CLOSE_ON_ACTIVE_TAB)
#                event.Skip()
            elif self.nb.GetPageCount() == event.GetSelection()+1:
                self.SetToolTip(wx.ToolTip("adfad"))
#                self.nb.SetWindowStyle(aui.AUI_NB_RIGHT | aui.AUI_NB_CLOSE_ON_ACTIVE_TAB)
#                event.Skip()
#                self.openTab()
            else:
                self.nb.SetWindowStyle(aui.AUI_NB_DEFAULT_STYLE)
                event.Skip()
    
    def chagedTab(self,event):
        print "chagedTab", self.nb.GetPageCount(),event.GetSelection(),self.nb.GetSelection()
        if(event.GetSelection()==0):
            self.nb.SetWindowStyle(aui.AUI_NB_TOP | aui.AUI_NB_TAB_SPLIT | aui.AUI_NB_TAB_MOVE | aui.AUI_NB_SCROLL_BUTTONS);
            event.Skip()
        else:
            if self.nb.GetPageCount() == event.GetSelection()+1:
#                a, b = event
#                self.nb.DeletePage(b)
#                self.openTab()
                page = self.nb.GetPage(self.nb.GetSelection())
                print 'aaa'
                page.SetToolTipString("adsfad")
                print 'aaa'
                
                self.menupopup = wx.Menu()
                
                idOpen = wx.NewId()
                item = wx.MenuItem(self.menupopup, idOpen, "Open")
                self.Bind(wx.EVT_MENU, self.openTab, id = idOpen)
                self.menupopup.AppendItem(item)
                
                for index in range(self.nb.GetPageCount()-1):
                    pageText = self.nb.GetPageText(index)
                    idOpen = wx.NewId()
                    item = wx.MenuItem(self.menupopup, idOpen, pageText)
                    self.Bind(wx.EVT_MENU, self.openTab, id = idOpen)
                    self.menupopup.AppendItem(item)
                #        bmp = wx.ArtProvider.GetBitmap(wx.ART_COPY, wx.ART_MENU, (24,24))
                #        item.SetBitmap(bmp)
                
                
                x,y  =self.GetPosition()
                x1,y1 = wx.GetMousePosition()
                self.PopupMenu(self.menupopup,(x1 - x - 10, 45))
                self.menupopup.Destroy()
                
                #self.nb.SetSelection(event.GetSelection())
            else:
                self.nb.SetWindowStyle(aui.AUI_NB_DEFAULT_STYLE)
                event.Skip()
                
                
    def tabClose(self,event):
        print "tabClose", self.nb.GetPageCount(),event.GetSelection(),self.nb.GetSelection()
        event.Skip()
        return
        if self.nb.GetPageCount() == event.GetSelection()+2:
            self.flagAddTab = True
            event.Skip() 
        else:
            self.flagAddTab = False
            event.Skip()            
        #event.Skip()
        
app = wx.PySimpleApp()
notepad().Show()
app.MainLoop()